const query = require('../util/sql')
module.exports = async (ctx, next) => {
	// sleep("2000");
	let param = {
		pageSize:ctx.request.query.pageSize?ctx.request.query.pageSize:3,
		pageNo:ctx.request.query.pageNo?ctx.request.query.pageNo:1,
		type:ctx.request.query.type
	}
	let sql;
	if (!param.type) {
		sql = `select * from news where type=1 and isDel=0 order by id desc limit ${(param.pageNo-1)*param.pageSize},${param.pageSize}`;
		let data1 = await query(sql)
		sql = `select * from news where type=2 and isDel=0 order by id desc limit ${(param.pageNo-1)*param.pageSize},${param.pageSize}`;
		let data2 = await query(sql)
		sql = `select * from news where type=3 and isDel=0 order by id desc limit ${(param.pageNo-1)*param.pageSize},${param.pageSize}`;
		let data3 = await query(sql)
		sql = `select count(*) count from news where type=1 and isDel=0`;
		let count1 = await query(sql)
		sql = `select count(*) count from news where type=2 and isDel=0`;
		let count2 = await query(sql)
		sql = `select count(*) count from news where type=3 and isDel=0`;
		let count3 = await query(sql)
		ctx.state.data = {count:[count1[0].count,count2[0].count,count3[0].count],data:[data1, data2, data3]}
	} else {
		sql = `select * from news where type=${param.type} and isDel=0 order by id desc limit ${(param.pageNo-1)*param.pageSize},${param.pageSize}`;
		let data = await query(sql)
		sql = `select count(*) count from news where type=${param.type} and isDel=0`;
		let count = await query(sql);
		ctx.state.data = {count:count[0].count,data:data};
	}
}
